import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import java.awt.Color;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JScrollBar;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.event.AdjustmentListener;
import java.awt.event.AdjustmentEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.HierarchyBoundsAdapter;
import java.awt.event.HierarchyEvent;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;

/**
 * =================================================================================
 * GENERAL NOTES
 * =================================================================================
 * - The bounds of each component in the ImageGallery is calculated 
 * based on the bounds of the ImageGallery. e.g. the height of a 
 * component is always set to be 20% of the height of the ImageGallery. 
 * Likewise, the x position of a component is always set to be half of 
 * the width of ImageGallery minus 20 (width of component = (getWidth / 2) - 20). 
 * This way, the layout and proportions of the components in ImageGallery 
 * will always be the same no matter what the bounds of the ImageGallery are.
 * =================================================================================
 *
 */

public class ImageGallery extends JPanel {
    
	/**
	 * Create the panel.
	 */
	private final JPanel panel;
	private final JPanel imageContainer;
	private Workspace ws;
	private String imageSelected = null;
	private IconResizer iconResizer;
	
	public ImageGallery(int x, int y, int width, int height) {
		setLayout(null);
		setBounds (x, y, width, height); // setting bounds to ImageGallery
		
		panel = new JPanel();
		panel.addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
			@Override
			public void ancestorResized(HierarchyEvent arg0) {
				panel.setSize(211, getHeight() - 70);
			}
		});
		panel.setBackground(Color.WHITE);
		panel.setBounds(10, 27, 211, getHeight() - 70); // setting bounds to panel
		imageContainer = new JPanel();
		imageContainer.setBackground(Color.WHITE);
		imageContainer.setBounds(27, 0, panel.getWidth() - 27, 10);
		imageContainer.setLayout(null);
		panel.add(imageContainer);
		add(panel);
		panel.setLayout(null);
		
		iconResizer = new IconResizer(null);
		

		
		
		
		File directory = new File ("src/gallery");
		String[] files = directory.list();
		
		
		
        for (int i = 0; i < files.length; i++){
        	if (files[i].endsWith(".jpg") || files[i].endsWith(".png")){
        	   imageContainer.setSize(imageContainer.getWidth(), imageContainer.getHeight() + 110);
        	   JButton btnNewButton = new JButton("");
        	   btnNewButton.setBounds(0, imageContainer.getHeight() - 110, 170, 100);
        	   ImageIcon image = new ImageIcon("src/gallery/" + files[i]);
        	   iconResizer.setImage(image);
        	   iconResizer.resize(btnNewButton.getHeight(), btnNewButton.getWidth());
        	   btnNewButton.setIcon(iconResizer.getImage());
        	   final String fileName = files[i];
        	   btnNewButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mousePressed(MouseEvent arg0) {
					    imageSelected = "src/gallery/" + fileName;
					}
				});
        	   imageContainer.add(btnNewButton);
        	   btnNewButton.updateUI();
        	}
		}
		final JScrollBar scrollBar = new JScrollBar();
		scrollBar.addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
			@Override
			public void ancestorResized(HierarchyEvent arg0) {
				// execute code below when ImageGallery is resized
				// set scrollBar height to panel height
				scrollBar.setSize(17, panel.getHeight());
			}
		});
		scrollBar.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				imageContainer.setLocation(imageContainer.getX(), 0 - scrollBar.getValue());
			}
		});
		scrollBar.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent arg0) {
				imageContainer.setLocation(imageContainer.getX(), 0 - scrollBar.getValue());
			}
		});
		scrollBar.setBounds(0, 0, 17, panel.getHeight());
		int maxScroll = imageContainer.getHeight() - panel.getHeight() + 10;
		if (maxScroll <= 0){
			scrollBar.setMaximum(0);
			panel.updateUI();
		} else {
			scrollBar.setMaximum(maxScroll);
			panel.updateUI();
		}
		panel.add(scrollBar);
		scrollBar.updateUI();
		
		final JButton btnInsertImage = new JButton("Insert Image");
		btnInsertImage.addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
			@Override
			public void ancestorResized(HierarchyEvent arg0) {
				// execute code below when ImageGallery is resized;
				btnInsertImage.setLocation(10, getHeight() - 33);
				int newMaxScroll = imageContainer.getHeight() - panel.getHeight() + 10;
				if (newMaxScroll <= 0){
					scrollBar.setMaximum(0);
					panel.updateUI();
				} else {
					scrollBar.setMaximum(newMaxScroll);
					panel.updateUI();
				}
			}
		});
		btnInsertImage.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				ws.setButtonImage(imageSelected);
			}
		});
		btnInsertImage.setBounds(10, getHeight() - 33, panel.getWidth() / 2, 23);
		add(btnInsertImage);
		
		final JButton uploadImage = new JButton("Upload Image");
		uploadImage.addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
			@Override
			public void ancestorResized(HierarchyEvent arg0) {
				// execute code below when ImageGallery is resized;
				uploadImage.setLocation((panel.getWidth() / 2) + 10, getHeight() - 33);
			}
		});
		uploadImage.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				JFileChooser uploadDialog = new JFileChooser();
				FileNameExtensionFilter fileFilter = new FileNameExtensionFilter("Image Files", "jpg", "jpeg", "bmp", "png", "gif");
				uploadDialog.setAcceptAllFileFilterUsed(false);
				uploadDialog.setFileFilter(fileFilter);
				int dialogReturn = uploadDialog.showDialog(getParent(), "Upload");
				if (dialogReturn == JFileChooser.APPROVE_OPTION){
					File file = uploadDialog.getSelectedFile();
					try {
						uploadImage (file);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		uploadImage.setBounds((panel.getWidth() / 2) + 10, getHeight() - 33, panel.getWidth() / 2, 23);
		add(uploadImage);
		
		this.repaint();
	}
	
	public void setWorkspace (Workspace workspace){
		ws = workspace;
	}
	
	// resize ImageGallery
	public void resizeImageGallery (){
		// height of panel re-adjusted;
		panel.setBounds(10, 27, 211, getHeight() - 70);
	}
	
	public void uploadImage (File file) throws IOException{
		final File output = new File ("src/gallery/" + file.getName());
		RenderedImage image = ImageIO.read(file);
		ImageIO.write(image, "jpg", output);
		imageContainer.setSize(imageContainer.getWidth(), imageContainer.getHeight() + 110);
		JButton btnNewButton = new JButton("");
		ImageIcon img = new ImageIcon(output.getPath());
 	    iconResizer.setImage(img);
 	    iconResizer.resize(100, 170);
 	    btnNewButton.setIcon(iconResizer.getImage());
 	    btnNewButton.setBounds(0, imageContainer.getHeight() - 110, 170, 100);
 	    btnNewButton.addMouseListener(new MouseAdapter() {
				 @Override
				 public void mousePressed(MouseEvent arg0) {
				     imageSelected = output.getPath();
				 }
		 	});
 	    imageContainer.add(btnNewButton);
 	    btnNewButton.updateUI();
	}
}
